﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;
using System.IO;

namespace SIOsystem
{
    public partial class ConfigForm : Form
    {
        public string uname = "";
        public long id = -1;
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            DictionaryClass.fillSetMainClass(ref SetMainClass);
            DictionaryClass.fillSetMainClass(ref NewMainClass);
            DictionaryClass.fillDepartment(ref DeptList);
            DictionaryClass.fillOperator(ref OperatorList);
        }

        private void SetMainClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SetMainClass.Items.Count > 0)
            {
                long MID = 0;
                if (SetMainClass.SelectedValue != null)
                {
                    long.TryParse(SetMainClass.SelectedValue.ToString(), out MID);
                }
                if (MID == -1)
                {
                    DictionaryClass.fillProductClass(ref ProductList, MID);
                }else if (MID > 0)
                {
                    DictionaryClass.fillProductClass(ref ProductList, MID);
                    MainClassName.Text = SetMainClass.Text;
                }
                else
                {
                    ProductList.DataSource = null;
                    MainClassName.Text = "";
                }
            }
        }

        private void ProductList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProductList.Items.Count > 0 && ((CheckedListBox)sender).DisplayMember!="")
            {
                long PID = 0;
                if (ProductList.SelectedValue != null)
                {
                    long.TryParse(ProductList.SelectedValue.ToString(), out PID);
                }
                if (PID > 0)
                {
                    DictionaryClass.fillModelClass(ref ModelList, PID,"[新增规格]");
                    NewProductName.Text = ProductList.Text;
                }
                else
                {
                    ModelList.DataSource = null;
                    NewProductName.Text = "";
                }
            }
        }

        private void ModelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ModelList.Items.Count > 0)
            {
                long TID = 0;
                if (ModelList.SelectedValue != null)
                {
                    long.TryParse(ModelList.SelectedValue.ToString(), out TID);
                }
                if (TID > 0)
                {
                    string[] pInfo = OrderClass.getProductInfo(TID);
                    NewTypeName.Text = ModelList.Text;
                    Price.Value = decimal.Parse(pInfo[1]);
                    Unit.Text = pInfo[2];
                }
                else
                {
                    Price.Value = 0;
                    Unit.Text = "";
                    NewTypeName.Text = "";
                }
            }
        }

        private void AddProduct_Click(object sender, EventArgs e)
        {
            if (ProductList.FindString(NewProductName.Text)>-1)
            {
               if (ModelList.FindString(NewTypeName.Text)>-1)
               {
                   MessageBox.Show("该规格已存在");
                   return;
               }
               else
               {
                   if (Price.Value == 0)
                   {
                       MessageBox.Show("请填写价格");
                       return;
                   }
                   if (Unit.Text == "")
                   {
                       MessageBox.Show("请填写单位");
                       return;
                   }
                   if (ClassifyClass.insertClassify(toLong(ProductList.SelectedValue), NewTypeName.Text
                       , double.Parse(Price.Value.ToString()), Unit.Text))
                   {
                       MessageBox.Show("规格添加成功！");
                   }
               }
            }
            else
            {
                if (NewProductName.Text == "")
                {
                    MessageBox.Show("请填写商品名");
                    return;
                }
                if (NewTypeName.Text == "")
                {
                    MessageBox.Show("请填写规格");
                    return;
                }
                if (Price.Value == 0)
                {
                    MessageBox.Show("请填写价格");
                    return;
                }
                if (Unit.Text == "")
                {
                    MessageBox.Show("请填写单位");
                    return;
                }
                if (ModelList.FindString(NewTypeName.Text) > -1)
                {
                    MessageBox.Show("该规格已存在");
                    return;
                }
                if (ProductList.FindString(NewProductName.Text) > -1)
                {
                    MessageBox.Show("该商品已存在");
                    return;
                }
                if (ClassifyClass.insertClassify(toLong(ProductList.SelectedValue), NewTypeName.Text
                    , double.Parse(Price.Value.ToString()), Unit.Text))
                {
                    MessageBox.Show("商品添加成功！");
                }
            }
            long MID = 0;
            if (SetMainClass.SelectedValue != null)
            {
                long.TryParse(SetMainClass.SelectedValue.ToString(), out MID);
            }
            if (MID > 0)
            {
                DictionaryClass.fillProductClass(ref ProductList, MID);
            }
        }

        private void AddMainClass_Click(object sender, EventArgs e)
        {
            if (SetMainClass.FindString(MainClassName.Text) > -1)
            {
                MessageBox.Show("该分类已存在，不能添加");
            }
            else
            {
                if (ClassifyClass.insertClassify(MainClassName.Text))
                {
                    MessageBox.Show("添加成功！");
                    DictionaryClass.fillSetMainClass(ref SetMainClass);
                    DictionaryClass.fillSetMainClass(ref NewMainClass);
                } 
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
        }

        private void ModMainClass_Click(object sender, EventArgs e)
        {
            if (SetMainClass.FindString(MainClassName.Text) > 0)
            {
                MessageBox.Show("该分类已存在，不能修改");
            }
            else
            {
                if (ClassifyClass.updateClassify(toLong(SetMainClass.SelectedValue),MainClassName.Text))
                {
                    MessageBox.Show("修改成功！");
                    DictionaryClass.fillSetMainClass(ref SetMainClass);
                    DictionaryClass.fillSetMainClass(ref NewMainClass);
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
        }

        private void DelMainClass_Click(object sender, EventArgs e)
        {
            if (ClassifyClass.deleteMainClass(toLong(SetMainClass.SelectedValue)))
            {
                MessageBox.Show("删除成功！");
                DictionaryClass.fillSetMainClass(ref SetMainClass);
                DictionaryClass.fillSetMainClass(ref NewMainClass);
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void ChangeClass_Click(object sender, EventArgs e)
        {
            if (ProductList.CheckedItems.Count>0)
            {
                long[] CID = new long[ProductList.CheckedItems.Count];
                int i=0;
                foreach (DataRowView dr in ProductList.CheckedItems)
                {
                    CID[i++] = long.Parse(dr["CID"].ToString());
                }
                if (ClassifyClass.ChangeParentById(CID, toLong(NewMainClass.SelectedValue)))
                {
                    DictionaryClass.fillSetMainClass(ref SetMainClass);
                    DictionaryClass.fillSetMainClass(ref NewMainClass);
                    MessageBox.Show("迁移成功！");
                }
            }
        }

        private void ProductName_TextChanged(object sender, EventArgs e)
        {
            if (NewProductName.Text!=ProductList.Text)
            {
                //ModelList.SelectedIndex = 0;
                NewTypeName.Text = "[常规]";
            }
        }

        private long toLong(object o)
        {
            return long.Parse(o.ToString());
        }

        private void ModProduct_Click(object sender, EventArgs e)
        {
            if (ProductList.FindString(NewProductName.Text) > -1)
            {
                if (ModelList.Text!=NewTypeName.Text && ModelList.FindString(NewTypeName.Text) > 0)
                {
                    MessageBox.Show("该规格已存在");
                    return;
                }
                if (Price.Value == 0)
                {
                    MessageBox.Show("请填写价格");
                    return;
                }
                if (Unit.Text == "")
                {
                    MessageBox.Show("请填写单位");
                    return;
                }
                if (ClassifyClass.updateClassify(toLong(ModelList.SelectedValue), NewTypeName.Text
                    , double.Parse(Price.Value.ToString()), Unit.Text))
                {
                    ClassifyClass.updateProductName(toLong(ModelList.SelectedValue), NewProductName.Text +" " +NewTypeName.Text);
                    MessageBox.Show("规格名与价格修改成功！");
                }
                long MID = 0;
                if (SetMainClass.SelectedValue != null)
                {
                    long.TryParse(SetMainClass.SelectedValue.ToString(), out MID);
                }
                if (MID > 0)
                {
                    DictionaryClass.fillProductClass(ref ProductList, MID);
                }
            }
        }

        private void initDB_Click(object sender, EventArgs e)
        {
            if (uname != "admin")
            {
                MessageBox.Show("只有管理员能初始化数据库");
                return;
            }
            ofd.InitialDirectory = @"c:\";
            ofd.RestoreDirectory = true;
            ofd.Filter = "初始库存数据|classify.csv";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(ofd.FileName,Encoding.Default);
                string classify;
                classify = sr.ReadToEnd();
                string[] line = classify.Replace("\r\n","\r").Split('\r');
                sr.Close();
                ClassifyClass.initDB(line);
            }
        }

        private void DeptList_SelectedIndexChanged(object sender, EventArgs e)
        {
            NewDept.Text = DeptList.Text;
        }

        private void AddDept_Click(object sender, EventArgs e)
        {
            if (DeptList.FindString(NewDept.Text)>-1)
            {
                MessageBox.Show("部门已存在！");
                return;
            }
            if (NewDept.Text.Length>0 && DeptClass.addDepartment(NewDept.Text))
            {
                MessageBox.Show("添加成功！");
                DictionaryClass.fillDepartment(ref DeptList);
            }
        }

        private void ModDept_Click(object sender, EventArgs e)
        {
            if (DeptList.FindString(NewDept.Text) > -1)
            {
                MessageBox.Show("部门已存在！");
                return;
            }
            if (DeptList.SelectedIndex != -1 && NewDept.Text.Length > 0 && DeptClass.modDepartment(NewDept.Text, toLong(DeptList.SelectedValue)))
            {
                MessageBox.Show("修改成功！");
                DictionaryClass.fillDepartment(ref DeptList);
            }
        }

        private void AddOperator_Click(object sender, EventArgs e)
        {
            if (uname != "admin")
            {
                MessageBox.Show("只有管理员能添加用户");
                return;
            }
            if (OperatorClass.addOperator(NewName.Text,NewPassword.Text))
            {
                MessageBox.Show("操作员添加成功！");
                DictionaryClass.fillOperator(ref OperatorList);
            }
        }

        private void ResetPassword_Click(object sender, EventArgs e)
        {
            if (MyNewPassword.Text.Trim().Length > 0 && OperatorClass.modPassword(MyNewPassword.Text, id))
            {
                MessageBox.Show("密码修改成功！");
            }
        }
    }
}
